/**
 * Created by zxr on 16/11/14.
 */
var multiparty = require('multiparty');
var fs = require('fs');
exports.getForm = function (req, res) {
	res.render('photos/upload', {title: 'Upload image'});
};
exports.submitForm = function (req, res, filed) {
	var form = new multiparty.Form({uploadDir: './public/photos/'});
	form.parse(req, function (err, fileds, files) {
		var filesTmp = JSON.stringify(files, null, 2);
		if (err) {
			console.log('parse error: ' + err);
		} else {
			console.log('parse files: ' + filesTmp);
			var inputFile = files.file[0];
			var uploadedPath = inputFile.path;
			var dstPath = './public/files/' + inputFile.originalFilename;
			//重命名为真实文件名
			fs.rename(uploadedPath, dstPath, function (err) {
				if (err) {
					console.log('rename error: ' + err);
				} else {
					console.log('rename ok');
				}
			});
		}
		res.render('photos/upload', {title: 'Upload image'});
	});

};